package com.bear.sgg.springboot_12_task.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassNmae EmailService
 * @Description TODO
 * @Author bear
 * @Date 2020/3/18 0018 上午 10:59
 */
@Service
public class EmailService {

    @Autowired
    JavaMailSenderImpl javaMailSender;

    /**
     * 发送简单的邮件
     */
    public void sendSimpleEmail() {

        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setSubject("通知-今晚开会");  //邮件标题
        smm.setText("今晚7：30开会");        //邮件内容

        smm.setTo("466168442@qq.com");      //接受者
        smm.setFrom("466168442@qq.com");    //发送者

        javaMailSender.send(smm);
    }

    /**
     * 发送复杂的邮件
     */
    public void sendMimeEmail() throws MessagingException {
        MimeMessage mmm = javaMailSender.createMimeMessage();
        //multipart属性表示可以发送附件
        MimeMessageHelper helper = new MimeMessageHelper(mmm, true);

        helper.setSubject("通知-今晚开party");
        helper.setText("<b style='color:red'>通知：今晚7.：30开party</b>",true);

        helper.setTo("466168442@qq.com");      //接受者
        helper.setFrom("466168442@qq.com");    //发送者

        String path = this.getClass().getResource("/static").getPath();

        //上传文件
        helper.addAttachment("1.jpg", new File(path + "/1.jpg"));
        helper.addAttachment("2.jpg", new File(path + "/2.jpg"));

        javaMailSender.send(mmm);
    }
}

